package com.kenyi.co.ui.me.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kenyi.co.GlideApp;
import com.kenyi.co.R;
import com.kenyi.co.common.ui.BaseActivity;
import com.kenyi.co.okhttp.OkHttpUtils;
import com.kenyi.co.okhttp.callback.StringCallback;
import com.kenyi.co.okhttp.utils.GsonResultok;
import com.kenyi.co.utils.CommonUtils;
import com.kenyi.co.utils.GlideCircleBorderTransform;
import com.kenyi.co.utils.LogU;
import com.kenyi.co.utils.NetConfig;
import com.kenyi.co.utils.SharepUtils;
import com.kenyi.co.utils.StringUtils;
import com.kenyi.co.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

import static com.kenyi.co.utils.CommonUtils.getAppVersion;


/**
 * 设置页面
 */
public class MeMainActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "SettingActivity";
    private TextView tv_home_title;
    private TextView tv_user_fav_1;
    private ImageView iv_me_avatar;
    private Toolbar toolbar;
    private RelativeLayout re_me_setting_avatar, re_me_setting_huancun, re_me_setting_tags;
    private TextView tv_user_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_main);
        initView();


    }

    @Override
    protected void onRestart() {
        super.onRestart();
        RequestOptions myOptions = new RequestOptions()
                .placeholder(R.drawable.iv_me_avatar)
                .optionalTransform
                        (new GlideCircleBorderTransform(CommonUtils.dip2px(this, 0), getResources().getColor(R.color.white)));
        GlideApp.with(this)
                .load(SharepUtils.getString_info(this, SharepUtils.USER_LOGIN_HEADADDRESS))
                .apply(myOptions)
                .into(iv_me_avatar);
        List<String> LikeTags;
        try {
            LikeTags = SharepUtils.getShDataList(MeMainActivity.this, SharepUtils.USER_FAV_TAGS_LIST, String.class);
            String tags = "";
            if (LikeTags != null && LikeTags.size() > 0) {
                for (int i = 0; i < LikeTags.size(); i++) {
                    if (i == 0) {
                        tags = LikeTags.get(i);
                    }
                    if (i == 1) {
                        tags = tags + " " + LikeTags.get(i);
                    }
                    if (i == 2) {
                        tags = tags + " " + LikeTags.get(i);
                    }
                }
                tv_user_fav_1.setText(tags);
                tv_user_fav_1.setVisibility(View.VISIBLE);
            } else {
                tv_user_fav_1.setVisibility(View.INVISIBLE);
            }
        }catch (Exception e){
            tv_user_fav_1.setVisibility(View.INVISIBLE);
        }

    }

    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.white));//设置状态栏颜色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        }
    }

    private void initView() {
        setStatusBar();
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
        tv_home_title = (TextView) findViewById(R.id.tv_home_title);
        tv_user_fav_1 = (TextView) findViewById(R.id.tv_tags);


        tv_home_title.setText("个人信息");
        re_me_setting_avatar = findViewById(R.id.re_me_setting_avatar);
        re_me_setting_huancun = findViewById(R.id.re_me_setting_huancun);
        re_me_setting_tags = findViewById(R.id.re_me_setting_tags);
        tv_user_name = findViewById(R.id.tv_user_name);
        iv_me_avatar = findViewById(R.id.iv_me_avatar);
        re_me_setting_avatar.setOnClickListener(this);
        re_me_setting_huancun.setOnClickListener(this);
        re_me_setting_tags.setOnClickListener(this);
        tv_user_name.setText(SharepUtils.getString_info(this, SharepUtils.USER_LOGIN_USERNAME));

        RequestOptions myOptions = new RequestOptions()
                .placeholder(R.drawable.iv_me_avatar)
                .optionalTransform
                        (new GlideCircleBorderTransform(CommonUtils.dip2px(this, 0), getResources().getColor(R.color.white)));
        GlideApp.with(this)
                .load(SharepUtils.getString_info(this, SharepUtils.USER_LOGIN_HEADADDRESS))
                .apply(myOptions)
                .into(iv_me_avatar);
        List<String> LikeTags;
        try {
            LikeTags = SharepUtils.getShDataList(MeMainActivity.this, SharepUtils.USER_FAV_TAGS_LIST, String.class);
            String tags = "";
            if (LikeTags != null && LikeTags.size() > 0) {
                for (int i = 0; i < LikeTags.size(); i++) {
                    if (i == 0) {
                        tags = LikeTags.get(i);
                    }
                    if (i == 1) {
                        tags = tags + " " + LikeTags.get(i);
                    }
                    if (i == 2) {
                        tags = tags + " " + LikeTags.get(i);
                    }
                }
                tv_user_fav_1.setText(tags);
                tv_user_fav_1.setVisibility(View.VISIBLE);
            } else {
                tv_user_fav_1.setVisibility(View.INVISIBLE);
            }
        }catch (Exception e){
            tv_user_fav_1.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public void onResume() {
        super.onResume();

        MobclickAgent.onPageStart("设置页面");
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();

        MobclickAgent.onPageEnd("设置页面");
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.re_me_setting_tags:
                Intent metags = new Intent(MeMainActivity.this, MeFavActivity.class);
                startActivity(metags);
                break;
            case R.id.re_me_setting_avatar:
                Intent melogin = new Intent(MeMainActivity.this, MeAvatarActivity.class);
                startActivity(melogin);
                break;
        }
    }


}
